package com.qnw.CardGroupManagement;
import android.content.*;
import android.content.res.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.view.*;
import android.support.v4.widget.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import com.ygo.feihua.*;
import java.util.*;

import android.support.v7.app.ActionBarDrawerToggle;
import com.ygo.feihua.R;
import java.io.*;
import android.util.*;
import com.ygo.feihua.util.*;
import com.qnw.CardGroupManagement.fragment.*;
import com.xiaomi.mistatistic.sdk.*;
import com.ygo.feihua.Management.*;


public class G5 extends AppCompatActivity 
{
	
	private ActionBarDrawerToggle mDrawerToggle;
	private DrawerLayout mDrawerlayout;
	private FragmentManager fragmentman;
		private FragmentCDB fragmentcdb;
	private FragmentDex fragmentdex;
		
		
		
	public Gongju gj;

	public String km;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g5_layout);

		
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		gj=Gongju.getdx(this);

		gj.cshToolbar(tool,"卡片编辑");
		tool.setOnMenuItemClickListener(onMenuItemClick);

		mDrawerlayout=(DrawerLayout) findViewById(R.id.drawerlayout);

		fragmentman=getSupportFragmentManager();
		fragmentdex=new FragmentDex();
		fragmentman.beginTransaction().add(R.id.flContent, fragmentdex).commit();
		fragmentcdb=(FragmentCDB) fragmentman.findFragmentById(R.id.menu_left);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerlayout, tool, R.string.drawer_open, R.string.drawer_close);

		//设置侧滑栏开启/关闭监听
		mDrawerlayout.setDrawerListener(new MyDrawerListener());
		
		
		//如果数据库路径中不存在数据库则把路径改为默认路径
		if(!new File(SharedPreferenceUtil.getCdbpath()).exists()){
			SharedPreferences sh=SharedPreferenceUtil.getSharePath();
			sh.edit().putString("cdbpath",Gongju.PATH_EZ_MANAGEMENT+"cards.cdb").commit();
		}

		//调整数据库
		File fff1=new File(Gongju.PATH_EZ_MANAGEMENT+"cards.cdb");
		if(!fff1.exists()){
			new Thread(new Runnable(){

					@Override
					public void run()
					{
						//assets中文件名字
						String fileName = "cards.cdb";
						try
						{
							//拿到输入流
							InputStream in = getAssets().open(fileName);
							//打开输出流
							FileOutputStream out = new FileOutputStream(Gongju.PATH_EZ_MANAGEMENT+fileName);
							int len = -1 ;
							byte[] bytes = new byte[1024];
							//不断读取输入流
							while ((len = in.read(bytes)) != -1)
							{
								//写到输出流中
								out.write(bytes, 0, len);
							}
							out.close();
							in.close();
						}
						catch (IOException e)
						{

						}
						// TODO: Implement this method
					}
				}).start();
					
					
		}
		
		Intent intent=getIntent();
		km = intent.getStringExtra("km");
		/*if (km != null && !km.equals(""))
		{
			//sxCard(km);
			Log.e("刷新卡片","刷新卡片");
		}*/

		String action = intent.getAction();
		if (intent.ACTION_VIEW.equals(action))
		{
			//数据路径
			String cdbPath=intent.getData().getPath();
			fragmentcdb.openCdb(cdbPath);

		}

		

	}


	@Override
	protected void onResume(){
		// TODO: Implement this method
		super.onResume();
		SdkInitManagement.getDx().initMiStats();
		MiStatInterface.recordPageStart(this, "数据库编辑");
	}

	@Override
	protected void onPause(){
		// TODO: Implement this method
		super.onPause();
		MiStatInterface.recordPageEnd();
	}
	
	
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
				case R.id.action_qd:				
					fragmentdex.save();				
					break;
			}
			// TODO: Implement this method
			return true;
		}
	};
	
	
	protected boolean isNavDrawerOpen() {
        return mDrawerlayout != null && mDrawerlayout.isDrawerOpen(GravityCompat.START);
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	
		getMenuInflater().inflate(R.menu.menu_qd, menu);
		
		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}

	
	public void sxCard(String km){
		fragmentdex.sxCard(km);
	}
	
	
	public void closeDrawer(){
	if (mDrawerlayout != null) {
		mDrawerlayout.closeDrawer(GravityCompat.START);
	}
	}
	/** activity创建完成后 */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
		/*   该方法会自动和actionBar关联, 将开关的图片显示在了action上，
		 如果不设置，也可以有抽屉的效果，不过是默认的图标 没有的话进去的默认是个箭头。。
		 正常应该是三横杠的
		 */ 
		mDrawerToggle.syncState();

    }
	/** 菜单键点击的事件处理 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
	}


	/** 设备配置改变时 */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
    }

	/** drawer的监听 */
	private class MyDrawerListener implements DrawerLayout.DrawerListener {
		@Override
		public void onDrawerOpened(View drawerView) {
			// 打开drawer
			mDrawerToggle.onDrawerOpened(drawerView);//开关状态改为opened
			drawerView.setClickable(true);
		}

		@Override
		public void onDrawerClosed(View drawerView) {
			// 关闭drawer
			mDrawerToggle.onDrawerClosed(drawerView);//开关状态改为closed
		
		}

		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {// drawer滑动的回调
			mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
		}

		@Override
		public void onDrawerStateChanged(int newState) {// drawer状态改变的回调
			mDrawerToggle.onDrawerStateChanged(newState);
		}
	}



	@Override
	public void onBackPressed()
	{
		if(fragmentcdb.isSelectedCard()||fragmentcdb.isSelectedCDB()){
			fragmentcdb.setSelectedCard(false);
			fragmentcdb.setSelectedCDB(false);
		}else{
			if(isNavDrawerOpen()){
				closeDrawer();
				}else{
				
			finish();
			}
		}
		// TODO: Implement this method
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode==NewWjxz.TYPE_PIC){
			fragmentdex.setkt(data.getStringExtra("path"));
		}
	}
	
}
